////
////  WLSelectView.swift
////  WLVehicleConfiguration
////
////  Created by Xiaochao Su 苏小超 on 2017/10/24.
////  Copyright © 2017年 Yongpeng.Zhu. All rights reserved.
////
//
//
//import UIKit
//
//public protocol WLSelectViewDelegate:class{
//    func numberOfRowsInSection(section: Int) -> Int
//    func cellForRowAt(indexPath: IndexPath) -> WLSelectorCell
//    func sizeForRowAt(indexPath: IndexPath) -> CGSize
//    func cellSpace(indexPath:IndexPath) -> CGFloat
//    func didSelectRowAt(model:WLResourceItem,indexPath:IndexPath)
//}
//
//public class WLSelectView: UIView {
//    @IBOutlet weak var contentView:UIView!
//    @IBOutlet weak var backWhiteView:UIView!
//    @IBOutlet weak var firstScrollView:WLScrollView!
//    @IBOutlet weak var secondScrollView:WLScrollView!
//    
//    @IBOutlet weak var topLine:UIView!
//    
//    public var selectedStyle: selectViewSelectedType? = .normal
//    
//    /// 上方偏移量,用于适配有材质时候的ScrollView滚动
//    public var topOffset:CGFloat = 0
//    
//    public var foldView : UIView?
//    public var bottomView: UIView?
//    public var angleView : WLConfigAngleView? //前后排View 传进来调整透明度
//    public var textureView:WLInteriorSelectView? // 材质view 传进来调整透明度
//    var currentScrollView:WLScrollView!//当前ScrollView
//    var hiddenScrollView:WLScrollView!//隐藏的ScrollView
//    public var scrollAnimateComplete = true //scroll动画状态
//    var scrollViewSection = 0 //当前选择的标签
//    public var selectCellIndex = 0 //当前选择的Cell
//    var isAnimationing = false //选择Cell动画状态
//    var alphaTimer : Timer? //全景按钮透明计时器
//    public var isSleep = false
//    //是否全屏
//    public var isFullScreen :Bool = false {
//        didSet{
//            if isFullScreen{
//                startVRMode()
//            }else{
//                stopVRMode()
//            }
//        }
//    }
//    var cells = [WLSelectorCell]()
//    ///
//    /// Selected 回调
//    ///
//    var selectedAction: (Int)->Void = { _ in }
//    
//    var clickBackgroundAction: (() -> Void)?
//    
//    public weak var delegate: WLSelectViewDelegate?{
//        didSet{
//            currentScrollView = self.secondScrollView
//            hiddenScrollView = self.firstScrollView
//            addCells()
//        }
//    }
//    
//    public func currentSelectedCell() -> WLSelectorCell?{
//        if selectCellIndex < cells.count{
//            return cells[selectCellIndex]
//        }
//        return nil
//    }
//    
//    public func cellForRow(at indexPath: IndexPath) -> WLSelectorCell?{
//        if indexPath.row < cells.count{
//            return cells[indexPath.row]
//        }
//        return nil
//    }
//    
//    
//    
//    
//    private func addCells(){
//        if let d = delegate {
//            self.cells = [WLSelectorCell]()
//            let count = d.numberOfRowsInSection(section: scrollViewSection)
//            for i in 0..<count{
//                let indexPath = IndexPath(row:i, section: scrollViewSection)
//                let cell = d.cellForRowAt(indexPath:indexPath)
//                let size = d.sizeForRowAt(indexPath: indexPath)
//                let width = size.width * CGFloat(count) + d.cellSpace(indexPath: indexPath) * CGFloat(count - 1)
//                let contentsize:CGSize = CGSize(width: width, height:1)
//                self.hiddenScrollView.contentSize = contentsize
//                
//                //如果contensize.width 小于
//                var left: CGFloat = 0
//                if width < UIScreen.main.bounds.width {
//                    left = (UIScreen.main.bounds.width - width)/2
//                }
//                
//                let x: CGFloat = CGFloat(i) * size.width + left + CGFloat(i) * d.cellSpace(indexPath: indexPath)
//                cell.frame = CGRect(x: x, y: self.size.height * (25/110), width: size.width, height: size.height)
//                cell.centerY = self.size.height / 2
//                cell.clickDelegate = self
//                self.cells.append(cell)
//                self.hiddenScrollView.addSubview(cell)
//                self.hiddenScrollView.layoutIfNeeded()
//            }
//        }
//    }
//    
//    /// 调整Cell居中
//    func adjustCellCenter(height:CGFloat){
//        for cell in cells{
//            cell.y = (height - cell.height) / 2
//        }
//    }
//
//    
//    public override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupView()
//    }
//    
//    required public init?(coder aDecoder: NSCoder) {
//        super.init(coder: aDecoder)
//        setupView()
//    }
//    
//    // Performs the initial setup.
//    private func setupView() {
//        let view = viewFromNibForClass()
//        view.frame = bounds
//        
//        // Auto-layout stuff.
//        view.autoresizingMask = [
//            UIView.AutoresizingMask.flexibleWidth,
//            UIView.AutoresizingMask.flexibleHeight
//        ]
//        firstScrollView.showsHorizontalScrollIndicator = false
//        firstScrollView.showsVerticalScrollIndicator = false
//        secondScrollView.showsHorizontalScrollIndicator = false
//        secondScrollView.showsVerticalScrollIndicator = false
//        firstScrollView.clipsToBounds = false
//        secondScrollView.clipsToBounds = false
//        backgroundColor = UIColor.clear
//        contentView.backgroundColor = UIColor.clear
//        firstScrollView.backgroundColor = UIColor.clear
//        secondScrollView.backgroundColor = UIColor.clear
//        
//        
//        firstScrollView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickBackground)))
//        secondScrollView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickBackground)))
//        
//        
//        // Show the view.
//        addSubview(view)
//        
//    }
//    
//    private func viewFromNibForClass() -> UIView {
//        
//        let bundle = vom_bundle
//        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
//        let view = nib.instantiate(withOwner: self, options: nil).first as? UIView
//        
//        return view!
//    }
//    
//    @objc func clickBackground() {
//        self.clickBackgroundAction?()
//        if self.isSleep{
//            self.stopTimer()
//            self.startTimer()
//            return
//        }
//    }
//    
//    /// 用于配置总览第一次准备,不需要进场动画的时候
//    public func noAnimate(){
//        if self.firstScrollView.contentSize.width >= self.frame.width{
//            self.firstScrollView.contentOffset.x = -25
//        }
//        self.firstScrollView.constant.constant = -UIScreen.main.bounds.width
//        self.layoutIfNeeded()
//        self.currentScrollView = self.firstScrollView
//        self.hiddenScrollView = self.secondScrollView
//    }
//    
//    public func animate(){
//        if self.firstScrollView.contentSize.width >= self.frame.width{
//            self.firstScrollView.contentOffset.x = -25
//        }
//        
//        UIView.animate(withDuration: 1, animations: {
//            self.firstScrollView.constant.constant = -UIScreen.main.bounds.width
//            
//            self.layoutIfNeeded()
//        }) { (finish) in
//            self.currentScrollView = self.firstScrollView
//            self.hiddenScrollView = self.secondScrollView
//        }
//    }
//    
//    func updateCellsAnimated(index:Int){
//       isAnimationing = true
//       let unselected =  cells[selectCellIndex]
//       let selected = cells[index]
//       selectCellIndex = index
//        
//        let leftRealX = selected.frame.maxX - currentScrollView.contentOffset.x
//        let leftOffset = leftRealX - self.frame.width
//        if leftOffset > 0 {
//            let point = CGPoint(x: currentScrollView.contentOffset.x + leftOffset + 25, y: 0)
//            self.currentScrollView.setContentOffset(point, animated: true)
//        }
//        
//        let rightRealX = selected.frame.origin.x - currentScrollView.contentOffset.x
//        if rightRealX < 0 {
//            let point = CGPoint(x: currentScrollView.contentOffset.x + rightRealX - 25, y: 0)
//            self.currentScrollView.setContentOffset(point, animated: true)
//        }
//        
//        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: [], animations: {
//            var newMultiplier:CGFloat = 0.786
//            var newMultiplier1:CGFloat = 0.5
//            
//            if self.selectedStyle == .large {
//                newMultiplier = 1
//                newMultiplier1 = 0.64
//            }
//            
//            selected.backgroundConstraintW = selected.backgroundConstraintW.setMultiplier(multiplier: newMultiplier)
//            selected.backgroundConstraintH = selected.backgroundConstraintH.setMultiplier(multiplier: newMultiplier)
//           
//            selected.innerConstraintW = selected.innerConstraintW.setMultiplier(multiplier: newMultiplier1)
//            selected.innerConstraintH = selected.innerConstraintH.setMultiplier(multiplier: newMultiplier1)
//
//            let newMultiplier2:CGFloat = 0.5
//            unselected.backgroundConstraintW = unselected.backgroundConstraintW.setMultiplier(multiplier: newMultiplier2)
//            unselected.backgroundConstraintH = unselected.backgroundConstraintH.setMultiplier(multiplier: newMultiplier2)
//
//            unselected.innerConstraintW = unselected.innerConstraintW.setMultiplier(multiplier: newMultiplier2)
//            unselected.innerConstraintH = unselected.innerConstraintH.setMultiplier(multiplier: newMultiplier2)
//
//            self.layoutIfNeeded()
//        }) { (finish) in
//            self.isAnimationing = false
//        }
//        
//        
//    }
//    
//    ///
//    /// 更新cells动画
//    ///
//    func updateCellsAnimated(selectedCell:WLSelectorCell) {
//        self.isAnimationing = true
//        for cell in self.cells {
//            cell.canSelect = false
//        }
//        for cell in cells {
//            if cell != selectedCell {
//                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
//                    cell.unselected()
//                    self.isAnimationing = false
//                }
//            }
//        }
//        
//        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
//            for cell in self.cells {
//                cell.canSelect = true
//            }
//        }
//    }
//    
//    public func scrollToSection(section:Int,topOffset:CGFloat = 0){
//        if scrollAnimateComplete{
//            self.topOffset = topOffset
//            selectCellIndex = 0
//            scrollViewSection = section
//            addCells()
//            changeScrollView(topOffset:topOffset)
//        }
//    }
//    
//    private func changeScrollView(topOffset:CGFloat,completion: (() -> Void)? = nil){
//        if(self.hiddenScrollView != self.currentScrollView && scrollAnimateComplete){
//            scrollAnimateComplete = false
//            
//            UIView.animate(withDuration: 1, animations: {
//                if self.hiddenScrollView.contentSize.width >= self.frame.width{
//                    self.hiddenScrollView.contentOffset.x = -25
//                }
//                self.currentScrollView.constant.constant = -UIScreen.main.bounds.width * 2
//                self.layoutIfNeeded()
//            }) { (finish) in
//                
//            }
//            
//            self.hiddenScrollView.scrollViewTop.constant = topOffset
//            self.layoutIfNeeded()
//            UIView.animate(withDuration: 1,delay:0.2, animations: {
//                self.hiddenScrollView.constant.constant = -UIScreen.main.bounds.width
//                self.layoutIfNeeded()
//            }) { (finish) in
//                self.contentView.bringSubview(toFront: self.currentScrollView)
//                //self.contentView.sendSubviewToBack( self.hiddenScrollView)
//                for view in self.currentScrollView.subviews{
//                    view.removeFromSuperview()
//                }
//                self.currentScrollView.constant.constant = 0
//                let temp = self.currentScrollView
//                self.currentScrollView = self.hiddenScrollView
//                self.hiddenScrollView = temp
//                self.scrollAnimateComplete = true
//            }
//            
//        }
//    }
//    
//    public func refreshView(section:Int,topOffset:CGFloat = 0){
//        if scrollAnimateComplete{
//            selectCellIndex = 0
//            scrollViewSection = section
//            addCells()
//            refreshViewAction(topOffset:topOffset)
//        }
//    }
//    
//    func refreshViewAction(topOffset:CGFloat) {
//        if(self.hiddenScrollView != self.currentScrollView && scrollAnimateComplete){
//            scrollAnimateComplete = false
//            if self.hiddenScrollView.contentSize.width >= self.frame.width{
//                self.hiddenScrollView.contentOffset.x = -25
//            }
//            self.currentScrollView.constant.constant = -UIScreen.main.bounds.width * 2
//            self.hiddenScrollView.scrollViewTop.constant = topOffset
//            self.hiddenScrollView.constant.constant = -UIScreen.main.bounds.width
//            self.layoutIfNeeded()
//
//            self.contentView.bringSubview(toFront: self.currentScrollView)
//            for view in self.currentScrollView.subviews{
//                view.removeFromSuperview()
//            }
//            self.currentScrollView.constant.constant = 0
//            let temp = self.currentScrollView
//            self.currentScrollView = self.hiddenScrollView
//            self.hiddenScrollView = temp
//            self.scrollAnimateComplete = true
//        }
//    }
//    
//    @objc private func changeAlpha(){
//        UIView.animate(withDuration: 0.5, animations: {
//            self.backWhiteView.alpha = 0.1
//            self.currentScrollView.alpha = 0.1
//            self.textureView?.backView.alpha = 0.1
//            self.textureView?.collectionView.alpha = 0.1
//            self.angleView?.alpha = 0.5
//            self.foldView?.alpha = 0.3
//            self.bottomView?.alpha = 0.3
//        }, completion: { (finish) in
//            self.isSleep = true
//        })
//    }
//    
//    private func awakeView(){
//        self.isSleep = false
//        UIView.animate(withDuration: 0.1) {
//            self.backWhiteView.alpha = 0.5
//            self.currentScrollView.alpha = 1
//            self.textureView?.backView.alpha = 0.4
//            self.textureView?.collectionView.alpha = 1
//            self.angleView?.alpha = 1
//            self.foldView?.alpha = 0.5
//            self.bottomView?.alpha = 0.5
//        }
//    }
//    
//    public func startTimer() {
//        if alphaTimer == nil {
//            alphaTimer =  Timer.scheduledTimer(
//                timeInterval: TimeInterval(3),
//                target      : self,
//                selector    : #selector(changeAlpha),
//                userInfo    : nil,
//                repeats     : true)
//        }
//    }
//    
//    public func stopTimer() {
//        if alphaTimer != nil {
//            alphaTimer?.invalidate()
//            alphaTimer = nil
//        }
//        awakeView()
//    }
//    
//    
//    func startVRMode(){
//        self.backWhiteView.isHidden = false
//        self.backWhiteView.alpha = 0.5
//        self.textureView?.backView.alpha = 0.4
//        for cell in self.cells {
//            cell.outerView.image = UIImage(named: "configure_white_shadow", in: nil, compatibleWith: nil)
//        }
//        if let cell = cells[safe:selectCellIndex]{
//            cell.selectedStyle = self.selectedStyle
//            cell.noAnimatedSelected()
//        }
//        startTimer()
//    }
//    
//    func stopVRMode() {
//        self.backWhiteView.isHidden = true
//        self.backWhiteView.alpha = 1
//        self.textureView?.backView.alpha = 1
//        for cell in self.cells {
//            cell.outerView.image = UIImage(named: "configure_gray_shadow", in: nil, compatibleWith: nil)
//        }
//        if let cell = cells[safe:selectCellIndex]{
//            cell.selectedStyle = self.selectedStyle
//            cell.noAnimatedSelected()
//        }
//        stopTimer()
//    }
//    
//
//}
//
//extension WLSelectView:WLCellButtonClickDelegate{
//    public func didSelectRowAt(model: WLResourceItem, indexPath: IndexPath) {
//        if self.isAnimationing || self.selectCellIndex == indexPath.row{
//            return
//        }
//        if self.isSleep{
//            self.stopTimer()
//            self.startTimer()
//            return
//        }
//        if self.isFullScreen{
//            self.stopTimer()
//            self.startTimer()
//        }
//        self.updateCellsAnimated(index: indexPath.row)
//        delegate?.didSelectRowAt(model: model, indexPath: indexPath)
//    }
//}
